package com.dx.mes.service.spc.service;


import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.ISpcBisChartMainDao;
import com.dx.mes.common.dao.ISpcBisGrpMainDao;
import com.dx.mes.common.dao.ISpcRetChartInfoDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.service.spc.tx.Spcchtmn.*;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Service("fqpchartmainService")
public class FqpchartmainService implements IFqpchartmainService {
	private LogUtils logUtils;
	
	@Autowired
	private ISpcBisChartMainDao spcBisChartMainDao;
	@Autowired
	private ISpcRetChartInfoDao spcRetChartInfoDao;
	@Autowired
	private ISpcBisGrpMainDao spcBisGrapMainDao;
	
	public String subMainProc(String evt_no,String strInMsg){
		AppContext.clear();
		AppContext.setCurrServiceName(FqpchartmainService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FqpchartmainService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
		
		String strOutTrx="";
		SpcchtmnI spcchtmnI = new SpcchtmnI();
		SpcchtmnO outTrx = new SpcchtmnO();
		outTrx.setTrx_id("FQPCHTMN");
		outTrx.setType_id("O");
		try{
			spcchtmnI = JacksonUtil.fromJson(strInMsg, SpcchtmnI.class);
			char action_flg = spcchtmnI.getAction_flg().charAt(0);
			switch( action_flg ){
				case ACTION_FLG_QUERY   :  queryByCondition(spcchtmnI, outTrx)  ; break;   
				case ACTION_FLG_ADD     :  addFnc(spcchtmnI, outTrx)            ; break;
				case 'C'     :  addFnc(spcchtmnI, outTrx)            ; break;
				case ACTION_FLG_UPDATE  :  updateFnc(spcchtmnI, outTrx)         ; break;
				case ACTION_FLG_DELETE  :  deleteFnc(spcchtmnI, outTrx)         ; break;
				case ACTION_FLG_INQUIRE :  inquireChartSettingFnc(spcchtmnI, outTrx);break;
				case ACTION_FLG_REPLACE :  updateChartSettingFnc(spcchtmnI, outTrx);break;
				default                 :  errActionFnc(outTrx);   
			}
			if( RETURN_CODE_OK.equals(outTrx.getRtn_code()) ){
				spcRetChartInfoDao.flushCurSession();
				outTrx.setRtn_mesg(RETURN_MESG_OK);
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}catch(Exception ex){
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			logUtils.error(Util.stackTraceToString(ex));
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}finally{
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}
	
	public void addFnc(SpcchtmnI spcchtmnI, SpcchtmnO outTrx)  {
		SpcchtmnIA iary = spcchtmnI.getIary().get(0);
		String col_typ_fk = iary.getCol_typ_fk();
		String grp_no_fk  = iary.getGrp_no_fk();
		String dataGroup = iary.getData_group_fk();
		
		String hql ="FROM Spc_bis_chart_main where col_typ_fk='" + iary.getCol_typ_fk() +
		            "' and grp_no_fk='"+iary.getGrp_no_fk()+
		            "' and chart_name='"+iary.getChart_name() +"'";
		List<Spc_bis_chart_main> spc_bis_chart_mainList = spcBisChartMainDao.find(hql);
		if(spc_bis_chart_mainList!=null && spc_bis_chart_mainList.size()!=0){
			outTrx.setRtn_code(E_FQPCHTMN_ALREDY_EXIST+"");
			outTrx.setRtn_mesg("chart["+iary.getChart_name()+"]"+"already Exist,please Check");
			return;
		}
		String chart_no = getMaxChartNoFnc(col_typ_fk,grp_no_fk);
		Spc_bis_chart_mainId chartMainId = new Spc_bis_chart_mainId();
		chartMainId.setCol_typ_fk(col_typ_fk);
		chartMainId.setGrp_no_fk(grp_no_fk);
		chartMainId.setChart_no(chart_no);
		Spc_bis_chart_main spc_bis_chart_main = new Spc_bis_chart_main();
		spc_bis_chart_main.setId(chartMainId);
		spc_bis_chart_main.setChart_typ(iary.getChart_typ());
		
		spc_bis_chart_main.setData_group_fk(dataGroup);
		spc_bis_chart_main.setChart_name(iary.getChart_name());
		spc_bis_chart_main.setChart_dsc(iary.getChart_dsc());
		spc_bis_chart_main.setChart_owner(iary.getChart_owner());
		spc_bis_chart_main.setChart_sub_typ(iary.getChart_sub_typ());//单点上报
		spc_bis_chart_main.setMax_chart_point(iary.getMax_chart_point());//管制图点数
		spc_bis_chart_main.setEvt_usr(iary.getEvt_usr());
		spc_bis_chart_main.setEvt_timestamp(Util.getCurrentTimestamp());
		spc_bis_chart_main.setRaw_keep_flg(iary.getRaw_keep_flg());//保留原始数据
		spc_bis_chart_main.setSample_size(iary.getSample_size());//样本数
		spcBisChartMainDao.save(spc_bis_chart_main);
		
		List<SpcchtmnIB> iaryBList = spcchtmnI.getIaryB();
		if(iaryBList==null){
			outTrx.setRtn_code(RETURN_CODE_OK);
			return;
		}
		for(SpcchtmnIB iaryB : iaryBList){
			Spc_ret_chart_info spc_ret_chart_info = new Spc_ret_chart_info();
			Spc_ret_chart_infoId chart_infoId = new Spc_ret_chart_infoId();
			chart_infoId.setCol_typ_fk(col_typ_fk);
			chart_infoId.setGrp_no_fk(grp_no_fk);
			chart_infoId.setChart_no_fk(chart_no);
			chart_infoId.setChart_typ_fk(iaryB.getChart_typ_fk());
			spc_ret_chart_info.setId(chart_infoId);
			spc_ret_chart_info.setData_group_fk(iaryB.getData_group_fk());
			spc_ret_chart_info.setChart_ucl(iaryB.getChart_ucl());
			spc_ret_chart_info.setChart_upl(iaryB.getChart_upl());
			spc_ret_chart_info.setChart_usl(iaryB.getChart_usl());
			spc_ret_chart_info.setChart_mean(iaryB.getChart_target());
			spc_ret_chart_info.setChart_target(iaryB.getChart_target());
			spc_ret_chart_info.setChart_lcl(iaryB.getChart_lcl());
			spc_ret_chart_info.setChart_lpl(iaryB.getChart_lpl());
			spc_ret_chart_info.setChart_lsl(iaryB.getChart_lsl());
			spc_ret_chart_info.setChart_rule_1_info(iaryB.getChart_rule_1_info());
			spc_ret_chart_info.setChart_rule_1_action(iaryB.getChart_rule_1_action());
			spc_ret_chart_info.setChart_rule_1_param(iaryB.getChart_rule_1_param());
			spc_ret_chart_info.setChart_rule_2_info(iaryB.getChart_rule_2_info());
			spc_ret_chart_info.setChart_rule_2_action(iaryB.getChart_rule_2_action());
			spc_ret_chart_info.setChart_rule_2_param(iaryB.getChart_rule_2_param());
			spc_ret_chart_info.setChart_spec_chk_flg(iaryB.getChart_spec_chk_flg());
			spc_ret_chart_info.setChart_show_multi_power(iaryB.getChart_show_multi_power());
			spc_ret_chart_info.setChart_ack_flg(iaryB.getChart_ack_flg());
			spcRetChartInfoDao.save(spc_ret_chart_info);
		}
	
		List<SpcchtmnOA> oaryList = new ArrayList<SpcchtmnOA>();
		SpcchtmnOA oary = new SpcchtmnOA();
		
		oary.setCol_typ_fk(spc_bis_chart_main.getId().getCol_typ_fk());
		oary.setGrp_no_fk(spc_bis_chart_main.getId().getGrp_no_fk());
		oary.setChart_no(spc_bis_chart_main.getId().getChart_no());
		oary.setChart_name(spc_bis_chart_main.getChart_name());
		oary.setChart_dsc(spc_bis_chart_main.getChart_dsc());
		oary.setChart_owner(spc_bis_chart_main.getChart_owner());
		oary.setChart_typ(spc_bis_chart_main.getChart_typ());
		oary.setData_group_fk(spc_bis_chart_main.getData_group_fk());
		oary.setSample_size(spc_bis_chart_main.getSample_size());
		oary.setMax_chart_point(spc_bis_chart_main.getMax_chart_point());
		oary.setRaw_keep_flg(spc_bis_chart_main.getRaw_keep_flg());
		oary.setChart_sub_typ(spc_bis_chart_main.getChart_sub_typ());
		oaryList.add(oary);
		
		outTrx.setOary(oaryList);
		outTrx.setRtn_code(RETURN_CODE_OK);
	}

	public void updateFnc(SpcchtmnI spcchtmnI, SpcchtmnO outTrx){
		SpcchtmnIA iary = spcchtmnI.getIary().get(0);
		String col_typ_fk = iary.getCol_typ_fk();
		String grp_no_fk  = iary.getGrp_no_fk();
		String chart_no   = iary.getChart_no();
		Spc_bis_chart_mainId chartMainId = new Spc_bis_chart_mainId();
		chartMainId.setCol_typ_fk(col_typ_fk);
		chartMainId.setGrp_no_fk(grp_no_fk);
		chartMainId.setChart_no(chart_no);
		
		Spc_bis_chart_main spc_bis_chart_main = spcBisChartMainDao.get(chartMainId);
		if( spc_bis_chart_main == null){
			outTrx.setRtn_code(E_FQPCHTMN_NOT_FOUND_RECORD+"");
			outTrx.setRtn_mesg("chart["+iary.getChart_name()+"]"+"not found,please Check");
			return;
		}

		if(iary.getChart_name()!=null){
			spc_bis_chart_main.setChart_name(iary.getChart_name());
		}
		if(iary.getChart_dsc()!=null){
			spc_bis_chart_main.setChart_dsc(iary.getChart_dsc());
		}
		if(iary.getChart_owner()!=null){
			spc_bis_chart_main.setChart_owner(iary.getChart_owner());
		}
		if(iary.getMax_chart_point()!=null){
			spc_bis_chart_main.setMax_chart_point(iary.getMax_chart_point());
		}else{
			Integer maxChartPoint = null;
			spc_bis_chart_main.setMax_chart_point(maxChartPoint);
		}

		if(iary.getRaw_keep_flg()!=null){
			spc_bis_chart_main.setRaw_keep_flg(iary.getRaw_keep_flg());
		}
		if(iary.getSample_size()!=null){
			spc_bis_chart_main.setSample_size(iary.getSample_size());
		}else{
			Integer sampleSize = null;
			spc_bis_chart_main.setSample_size(sampleSize);
		}
		spc_bis_chart_main.setEvt_usr(iary.getEvt_usr());
		spc_bis_chart_main.setEvt_timestamp(Util.getCurrentTimestamp());
//		spc_bis_chart_main.setOoc_ocap(iary.getOoc_ocap());
//		spc_bis_chart_main.setOos_ocap(iary.getOos_ocap());
		spcBisChartMainDao.update(spc_bis_chart_main);
		
		List<SpcchtmnIB> iaryBList = spcchtmnI.getIaryB();
		if(iaryBList==null){
			outTrx.setRtn_code(RETURN_CODE_OK);
			return;
		}
		for(SpcchtmnIB iaryB : iaryBList){
			
			Spc_ret_chart_infoId chart_infoId = new Spc_ret_chart_infoId();
			chart_infoId.setCol_typ_fk(col_typ_fk);
			chart_infoId.setGrp_no_fk(grp_no_fk);
			chart_infoId.setChart_no_fk(chart_no);
			chart_infoId.setChart_typ_fk(iaryB.getChart_typ_fk());
			Spc_ret_chart_info spc_ret_chart_info = spcRetChartInfoDao.get(chart_infoId);
			spc_ret_chart_info.setId(chart_infoId);
			spc_ret_chart_info.setChart_ucl(iaryB.getChart_ucl());
			spc_ret_chart_info.setChart_upl(iaryB.getChart_upl());
			spc_ret_chart_info.setChart_usl(iaryB.getChart_usl());
			spc_ret_chart_info.setChart_mean(iaryB.getChart_target());
			spc_ret_chart_info.setChart_target(iaryB.getChart_target());
			spc_ret_chart_info.setChart_lcl(iaryB.getChart_lcl());
			spc_ret_chart_info.setChart_lpl(iaryB.getChart_lpl());
			spc_ret_chart_info.setChart_lsl(iaryB.getChart_lsl());
//			spc_ret_chart_info.setChart_rule_1_info(iaryB.getChart_rule_1_info());
			spc_ret_chart_info.setChart_rule_1_action(iaryB.getChart_rule_1_action());
			spc_ret_chart_info.setChart_rule_1_param(iaryB.getChart_rule_1_param());
			spc_ret_chart_info.setChart_rule_2_info(iaryB.getChart_rule_2_info());
			spc_ret_chart_info.setChart_rule_2_action(iaryB.getChart_rule_2_action());
			spc_ret_chart_info.setChart_rule_2_param(iaryB.getChart_rule_2_param());
			spc_ret_chart_info.setChart_spec_chk_flg(iaryB.getChart_spec_chk_flg());
			spc_ret_chart_info.setChart_show_multi_power(iaryB.getChart_show_multi_power());
			spc_ret_chart_info.setChart_ack_flg(iaryB.getChart_ack_flg());
			spcRetChartInfoDao.saveOrUpdate(spc_ret_chart_info);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
	}
	public void deleteFnc(SpcchtmnI spcchtmnI, SpcchtmnO outTrx){
		List<SpcchtmnIA> iaryList = spcchtmnI.getIary();
		for(SpcchtmnIA iary: iaryList){
			Spc_bis_chart_mainId chartMainId = new Spc_bis_chart_mainId();
			chartMainId.setCol_typ_fk(iary.getCol_typ_fk());
			chartMainId.setGrp_no_fk(iary.getGrp_no_fk());
			chartMainId.setChart_no(iary.getChart_no());
			Spc_bis_chart_main spc_bis_chart_main = spcBisChartMainDao.get(chartMainId);
			if(spc_bis_chart_main==null){
				outTrx.setRtn_code(E_FQPCHTMN_NOT_FOUND_RECORD+"");
				outTrx.setRtn_mesg("not found main group");
				return;
			}
			spcBisChartMainDao.delete(spc_bis_chart_main);
			String hql = "FROM Spc_ret_chart_info where col_typ_fk='" +iary.getCol_typ_fk() +
		      "' and grp_no_fk='"+ iary.getGrp_no_fk() +
		      "' AND chart_no_fk='"+ iary.getChart_no()+"'";
			List<Spc_ret_chart_info> spcRetChartInfoList = spcRetChartInfoDao.find(hql);
			for(Spc_ret_chart_info spc_ret_chart_info:spcRetChartInfoList){
				spcRetChartInfoDao.delete(spc_ret_chart_info);
			}
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
	}
	public void queryByCondition(SpcchtmnI spcchtmnI, SpcchtmnO outTrx){
		String hql = "FROM Spc_bis_chart_main where 1=1" ;
		SpcchtmnIA iary = spcchtmnI.getIary().get(0);
		if(iary.getCol_typ_fk()!=null){
			hql = hql +" and col_typ_fk='" +iary.getCol_typ_fk() +"'";
		}
		if(iary.getGrp_no_fk()!=null){
			hql = hql +" and grp_no_fk='" +iary.getGrp_no_fk() +"'";
		}
		if(iary.getChart_no()!=null){
			hql = hql +" and chart_no='" +iary.getChart_no() +"'";
		}
		if(iary.getData_group_fk()!=null){
			hql = hql +" and data_group_fk='" +iary.getData_group_fk() +"'";
		}
		List<Spc_bis_chart_main> spcBisChartMainList = spcBisChartMainDao.find(hql);
		List<SpcchtmnOA> oaryList = new ArrayList<SpcchtmnOA>();
		for(Spc_bis_chart_main spc_bis_chart_main :spcBisChartMainList){
			SpcchtmnOA oary = new SpcchtmnOA();
			oary.setCol_typ_fk(spc_bis_chart_main.getId().getCol_typ_fk());
			oary.setGrp_no_fk(spc_bis_chart_main.getId().getGrp_no_fk());
			oary.setChart_no(spc_bis_chart_main.getId().getChart_no());
			oary.setData_group_fk(spc_bis_chart_main.getData_group_fk());
			oary.setChart_name(spc_bis_chart_main.getChart_name());
			oary.setChart_dsc(spc_bis_chart_main.getChart_dsc());
			oary.setChart_owner(spc_bis_chart_main.getChart_owner());
			oary.setChart_typ(spc_bis_chart_main.getChart_typ());
			oary.setChart_sub_typ(spc_bis_chart_main.getChart_sub_typ());
			oary.setEvt_usr(spc_bis_chart_main.getEvt_usr());
			oary.setEvt_timestamp(spc_bis_chart_main.getEvt_timestamp());
			oary.setMax_chart_point(spc_bis_chart_main.getMax_chart_point());
			oary.setOos_ocap(spc_bis_chart_main.getOos_ocap());
			oary.setOoc_ocap(spc_bis_chart_main.getOoc_ocap());
			oary.setRaw_keep_flg(spc_bis_chart_main.getRaw_keep_flg());
			oary.setSample_size(spc_bis_chart_main.getSample_size());
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
	}
	public void inquireChartSettingFnc(SpcchtmnI spcchtmnI, SpcchtmnO outTrx){
		SpcchtmnIA iary = spcchtmnI.getIary().get(0);
		String hql_main = "FROM Spc_bis_chart_main where 1=1";
		if(iary.getCol_typ_fk()!=null){
			hql_main = hql_main +" and col_typ_fk='" +iary.getCol_typ_fk() +"'";
		}
		if(iary.getGrp_no_fk()!=null){
			hql_main = hql_main +" and grp_no_fk='" +iary.getGrp_no_fk() +"'";
		}
		if(iary.getChart_no()!=null){
			hql_main = hql_main +" and chart_no='" +iary.getChart_no() +"'";
		}
		if(iary.getChart_typ()!=null){
			hql_main = hql_main +" and chart_typ='"+iary.getChart_typ()+"'";
		}
		List<Spc_bis_chart_main> spcBisChartMainList = spcRetChartInfoDao.find(hql_main);
		String hql = "FROM Spc_ret_chart_info where 1=1" ;
		if(iary.getCol_typ_fk()!=null){
			hql = hql +" and col_typ_fk='" +iary.getCol_typ_fk() +"'";
		}
		if(iary.getGrp_no_fk()!=null){
			hql = hql +" and grp_no_fk='" +iary.getGrp_no_fk() +"'";
		}
		if(iary.getChart_no()!=null){
			hql = hql +" and chart_no_fk='" +iary.getChart_no() +"'";
		}
		if(iary.getChart_typ()!=null){
			hql = hql +" and chart_typ_fk='"+iary.getChart_typ()+"'";
		}
		hql =  hql + " order by chart_typ_fk";
		List<Spc_ret_chart_info> spcRetChartInfoList = spcRetChartInfoDao.find(hql);
		List<SpcchtmnOB> oaryBList = new ArrayList<SpcchtmnOB>();
		for(Spc_ret_chart_info spc_ret_chart_info:spcRetChartInfoList){
			SpcchtmnOB oaryB = new SpcchtmnOB();

			String colTypFk = spc_ret_chart_info.getId().getCol_typ_fk();
			String grpNoFk = spc_ret_chart_info.getId().getGrp_no_fk();
			String chartNoFk = spc_ret_chart_info.getId().getChart_no_fk();
			Spc_bis_chart_mainId spc_bis_chart_mainId= new Spc_bis_chart_mainId();
			spc_bis_chart_mainId.setCol_typ_fk(colTypFk);
			spc_bis_chart_mainId.setGrp_no_fk(grpNoFk);
			spc_bis_chart_mainId.setChart_no(chartNoFk);
			Spc_bis_chart_main spc_bis_chart_main = spcBisChartMainDao.get(spc_bis_chart_mainId);
			oaryB.setChart_name(spc_bis_chart_main.getChart_name());
			oaryB.setChart_dsc(spc_bis_chart_main.getChart_dsc());
			oaryB.setChart_typ_fk(spc_bis_chart_main.getChart_typ());
			//gname
			Spc_bis_grp_mainId spc_bis_grp_mainId= new Spc_bis_grp_mainId();
			spc_bis_grp_mainId.setCol_typ(colTypFk);
			spc_bis_grp_mainId.setGrp_no(grpNoFk);
			Spc_bis_grp_main spc_bis_grp_main = spcBisGrapMainDao.get(spc_bis_grp_mainId);
			oaryB.setGrp_name(spc_bis_grp_main.getGrp_name());
			oaryB.setCol_typ_fk(colTypFk);
			oaryB.setGrp_no_fk(grpNoFk);
			oaryB.setChart_no_fk(chartNoFk);
			oaryB.setChart_typ_fk(spc_ret_chart_info.getId().getChart_typ_fk());
			oaryB.setChart_spec_chk_flg(spc_ret_chart_info.getChart_spec_chk_flg());
			oaryB.setChart_rule_1_action(spc_ret_chart_info.getChart_rule_1_action());
			oaryB.setChart_rule_1_info(spc_ret_chart_info.getChart_rule_1_info());
			oaryB.setChart_rule_1_param(spc_ret_chart_info.getChart_rule_1_param());
			oaryB.setChart_rule_2_action(spc_ret_chart_info.getChart_rule_2_action());
			oaryB.setChart_rule_2_info(spc_ret_chart_info.getChart_rule_2_info());
			oaryB.setChart_rule_2_param(spc_ret_chart_info.getChart_rule_2_param());
			oaryB.setChart_calc_multi_power(spc_ret_chart_info.getChart_calc_multi_power());//倍率

			oaryB.setChart_lcl(spc_ret_chart_info.getChart_lcl());
			oaryB.setChart_lpl(spc_ret_chart_info.getChart_lpl());
			oaryB.setChart_lsl(spc_ret_chart_info.getChart_lsl());
			oaryB.setChart_mean(spc_ret_chart_info.getChart_mean());
			oaryB.setChart_target(spc_ret_chart_info.getChart_target());
			oaryB.setChart_ucl(spc_ret_chart_info.getChart_ucl());
			oaryB.setChart_upl(spc_ret_chart_info.getChart_upl());
			oaryB.setChart_usl(spc_ret_chart_info.getChart_usl());
			oaryB.setChart_rule_1_info(spc_ret_chart_info.getChart_rule_1_info());
			oaryB.setChart_rule_1_action(spc_ret_chart_info.getChart_rule_1_action());
			oaryB.setChart_rule_1_param(spc_ret_chart_info.getChart_rule_1_param());
			oaryB.setChart_rule_1_rlt(spc_ret_chart_info.getChart_rule_1_rlt());
			oaryB.setChart_ack_flg(spc_ret_chart_info.getChart_ack_flg());
			oaryBList.add(oaryB);
		}
		outTrx.setOaryB(oaryBList);
		outTrx.setTbl_cnt(oaryBList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
	}   
	public void updateChartSettingFnc(SpcchtmnI spcchtmnI, SpcchtmnO outTrx) {
		List<SpcchtmnIB> iaryBList = spcchtmnI.getIaryB();
		for (SpcchtmnIB iaryB : iaryBList) {
			String colTyp = iaryB.getCol_typ_fk();
			String grpNo = iaryB.getGrp_no_fk();
			String chartNo  = iaryB.getChart_no_fk();
			String chartTyp = iaryB.getChart_typ_fk();
			String chartRule = iaryB.getChart_rule_1_info();
			String hql = "from Spc_ret_chart_info where col_typ_fk = ? and grp_no_fk=? and chart_no_fk = ? and chart_typ_fk = ?";
			Spc_ret_chart_info spcRetChartInfo = spcRetChartInfoDao.uniqueResultWithLock(hql, colTyp,grpNo,chartNo,chartTyp);
			spcRetChartInfo.setChart_rule_1_info(chartRule);
			spcRetChartInfoDao.save(spcRetChartInfo);
		}
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	public void errActionFnc(SpcchtmnO outTrx){
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}
	public String getMaxChartNoFnc(String col_typ_fk,String grp_no_fk){
		String hql = "select max(id.chart_no) FROM Spc_bis_chart_main WHERE id.col_typ_fk='"+col_typ_fk+"'";
			   hql = hql + " and id.grp_no_fk='"+ grp_no_fk +"'";
	    Session session = spcBisChartMainDao.getCrSession();
		Query getMaxquery  = session.createQuery(hql);
		Object object = getMaxquery.uniqueResult();
		int iMaxChartNo=0;
		if(object!=null){
			iMaxChartNo = Integer.valueOf(object.toString());
    		return Util.comInt2String(iMaxChartNo+1,6);
		}
		return "000001";
	}
	
}


